package com.rongc.client.freight.base.view.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.SystemUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.model.UserBean;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.adapter.RecyclerMineAdapter;
import com.rongc.client.freight.base.view.widget.SpaceItemDecoration;
import com.rongc.client.freight.business.mine.view.activity.AuthHostActivity;
import com.rongc.client.freight.business.mine.view.activity.CarHostActivity;
import com.rongc.client.freight.business.mine.view.activity.MineWalletActivity;
import com.rongc.client.freight.business.mine.view.activity.SettingActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2016/11/24.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.recycler)
    RecyclerView recyclerView;
    @Bind(R.id.iv_head)
    ImageView mIvHead;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_tip)
    TextView mTvTip;
    @Bind(R.id.tv_wallet)
    TextView mTvWallet;
    @Bind(R.id.tv_integral)
    TextView mTvIntegral;

    UserBean userBean;
    RecyclerMineAdapter mRecyclerAdapter;

    List<String> lsOperate;
    SpaceItemDecoration itemDecoration ;
    @OnClick({R.id.tv_wallet,R.id.tv_integral})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_wallet:
                ActivityUtils.startActivity(getActivity(), MineWalletActivity.class);
                break;
            case R.id.tv_identity:
                break;
        }
    }

    @Override
    public void initData() {

        lsOperate.add(BaseDic.MineType.AUTH_TYPE);
        lsOperate.add(BaseDic.MineType.SHOP_TYPE);
        lsOperate.add(BaseDic.MineType.CAR_TYPE);
        lsOperate.add(BaseDic.MineType.CHECK_IN_TYPE);
        lsOperate.add(BaseDic.MineType.COMMENT_TYPE);
        lsOperate.add(BaseDic.MineType.SETTING_TYPE);

    }

    @Override
    public void initListener() {
        mRecyclerAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<String>() {
            @Override
            public void onItemClick(View view, int position, String data) {
                switch (data){
                    case BaseDic.MineType.AUTH_TYPE:
                        ActivityUtils.startActivity(getActivity(), AuthHostActivity.class);
                        break;
                    case BaseDic.MineType.SHOP_TYPE:
                        break;
                    case BaseDic.MineType.CAR_TYPE:
                        ActivityUtils.startActivity(getActivity(), CarHostActivity.class);
                        break;
                    case BaseDic.MineType.CHECK_IN_TYPE:
                        break;
                    case BaseDic.MineType.COMMENT_TYPE:
                        break;
                    case BaseDic.MineType.SETTING_TYPE:
                        ActivityUtils.startActivity(getActivity(), SettingActivity.class);
                        break;
                }
            }
        });
    }


    @Override
    public void initView() {
        userBean = UniApplication.getInstance().getUserInfo();
        lsOperate = new ArrayList<>();
        mRecyclerAdapter = new RecyclerMineAdapter(getActivity(),lsOperate);

        if(itemDecoration == null)
            itemDecoration = new SpaceItemDecoration(SystemUtil.dip2px(getContext(), 1f), SystemUtil.dip2px(getContext(), 1f));

        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        recyclerView.removeItemDecoration(itemDecoration);
        recyclerView.addItemDecoration(itemDecoration);
        recyclerView.setAdapter(mRecyclerAdapter);

        Glide.with(UniApplication.getInstance())
                .load(userBean.getHead())
                .placeholder(R.mipmap.auth_icon)
                .into(mIvHead);

        mTvName.setText(userBean.getNickname());
        mTvTip.setText(String.format(
                getContext().getResources().getString(R.string.mine_tip),
                userBean.getCountXD(),userBean.getCountJD(),userBean.getCountWY()));

        mTvIntegral.setText(Html.fromHtml(String.format(
                getContext().getResources().getString(R.string.mine_integral_title),
                userBean.getIntegral())));

        mTvWallet.setText(Html.fromHtml(String.format(
                getContext().getResources().getString(R.string.mine_wallet_title),
                userBean.getBalance())));
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_host_mine;
    }
}
